<?php
/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category Zeed
 * @package Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author Zeed Team (http://blog.zeed.com.cn)
 * @since 2010-12-6
 * @version SVN: $Id$
 */

class FolderController extends AttachmentAdminAbstract
{
    /**
     * 新建文件夹
     */
    public function mkdir()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $pid = (int) $this->input->query('pid');
        $card_id = $this->input->query('card_id');
        $title = $this->input->query('title', '新建文件夹');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isAJAX()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $pid || ! $card_id) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 查询父级目录是否存在 */
            $where_parent = array('fid' => $pid, 'card_id' => $card_id);
            $folder_info_parent = Attachment_Model_Folder::instance()->fetchByWhere($where_parent);
            if (empty($folder_info_parent)) {
                throw new Zeed_Exception('查无此父级目录');
            }
            
            /* 查询行动信息是否存在 */
            $card_info = Archive_Model_Card::instance()->fetchByPK($card_id);
            if (empty($card_info)) {
                throw new Zeed_Exception('查无此行动信息');
            }
            
            /* 创建文件夹 */
            $params_addfolder = array(
                    'parent_id' => $pid,
                    'userid' => $this->_userid_fs,
                    'title' => $title,
                    'sn' => $card_info[0]['sn'],
                    'card_id' => $card_id
            );
            $result = Attachment_Folder_Add::run($params_addfolder);
            if ($result['status'] != 0) {
                throw new Zeed_Exception($result['msg'], $result['status']);
            }
            $folder_info = $result['data'];
            
            /* @todo 记录日志 */
            
            
            $data['folder_info'][0] = $folder_info;
            $this->setData('data', $data);
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('创建失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 文件夹重命名
     */
    public function rename()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $folder_id = $this->input->query('folder_id');
        $title = $this->input->query('title');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isAJAX()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $folder_id || ! $title) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 执行更新 */
            $params_rename = array(
                    'userid' => $this->_userid_fs,
                    'folder_id' => $folder_id,
                    'title' => $title
            );
            $result = Attachment_Folder_Rename::run($params_rename);
            if ($result['status'] != 0) {
                throw new Zeed_Exception($result['msg'], $result['status']);
            }
            
            /* @todo 记录日志 */
            
            
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('重命名失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 删除文件夹
     */
    public function delete()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $fid = (int) $this->input->query('fid');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isAJAX()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $fid) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }

            /* 获取该目录信息 */
            $where_folder = array(
                'userid' => $this->_userid_fs,
                'fid' => $fid
            );
            $folder_info = Attachment_Model_Folder::instance()->fetchByWhere($where_folder);
            if (empty($folder_info)) {
                throw new Zeed_Exception('查无此文件夹信息');
            }

            /* 查询目录所属行动信息 */
            $card_info = Archive_Model_Card::instance()->fetchByPK($folder_info[0]['card_id']);
            if (empty($card_info)) {
                throw new Zeed_Exception('该文件夹所属行动不存在');
            }

            /* 整理出该行动下的所有上传文件信息 */
            $file_image = $file_audio = $file_video = array();
            if ($card_info[0]['attachments_img']) {
                $file_image = explode(',', $card_info[0]['attachments_img']);
            }
            if ($card_info[0]['attachments_audio']) {
                $file_audio = explode(',', $card_info[0]['attachments_audio']);
            }
            if ($card_info[0]['attachments_video']) {
                $file_video = explode(',', $card_info[0]['attachments_video']);
            }

            /* 查询该目录下的子级文件夹 */
            $where_folder_children = array(
                'userid' => $this->_userid_fs,
                'pid' => $fid
            );
            $folder_children = Attachment_Model_Folder::instance()->fetchByWhere($where_folder_children);

            /* 查询该目录下的子级文件 */
            $count_folder_file = 0;
            $where_folder_file = array(
                'folder_hid' => $folder_info[0]['hid']
            );
            $file_children = Attachment_Model_Content::instance()->fetchByWhere($where_folder_file);
            if (! empty($file_children)) {
                foreach ($file_children as $k => $v) {
                    if (in_array($v['_id'], $file_image) || in_array($v['_id'], $file_audio) || in_array($v['_id'], $file_video)) {
                        $count_folder_file++;
                    }
                }
            }

            /* 若该目录下有文件或文件夹，则不可删除 */
            if (! empty($folder_children) || $count_folder_file > 0) {
                throw new Zeed_Exception('该文件夹下包含有子文件夹或文件，请清空后再执行删除');
            }
            
            /* 执行删除 */
            $params_delete = array(
                    'userid' => $this->_userid_fs,
                    'folders' => $fid
            );
            $result = Attachment_Trash::run($params_delete);
            if ($result['status'] != 0) {
                throw new Zeed_Exception($result['msg'], $result['status']);
            }
            
            /* @todo 记录日志 */
            
            
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('删除失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 获取指定目录下的子目录及文件
     */
    public function getChildren()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $pid = (int) $this->input->query('pid');
        $card_id = $this->input->query('card_id');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isAJAX()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $pid || ! $card_id) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 查询父级目录是否存在 */
            $where_parent = array('fid' => $pid, 'card_id' => $card_id);
            $folder_info_parent = Attachment_Model_Folder::instance()->fetchByWhere($where_parent);
            if (empty($folder_info_parent)) {
                throw new Zeed_Exception('查无此父级目录');
            }
            
            /* 查询行动信息是否存在 */
            $card_info = Archive_Model_Card::instance()->fetchByPK($card_id);
            if (empty($card_info)) {
                throw new Zeed_Exception('查无此行动信息');
            }
            
            /* 查询子级目录信息 */
            $where_child_folder = array(
                    'parent_id' => $pid,
                    'userid' => $this->_userid_fs,
                    'card_id' => $card_id,
                    'status' => 1
            );
            $folder_info = Attachment_Model_Folder::instance()->fetchByWhere($where_child_folder);
            
            /* 处理子级目录信息 */
            if (! empty($folder_info)) {
                foreach ($folder_info as $k => $v) {
                    /* 处理时间 */
                    $folder_info[$k]['ctime'] = Util_Timer::formatMicrotime($v['ctime'], 'Y-m-d H:i:s');
                }
            }
            
            /* 查询子级文件信息 */
            $where_child_content = array(
                    'pid' => '',
                    'folder_hid' => $folder_info_parent[0]['hid'],
                    'status' => 1
            );
            $content_info = Attachment_Model_Content::instance()->fetchByWhere($where_child_content);
            
            /* 处理子级文件信息 */
            if (! empty($content_info)) {
                foreach ($content_info as $k => $v) {
                    /* 处理容量 */
                    $content_info[$k]['filesize_format'] = Util_File::formatFilesize($v['filesize']);
                    
                    /* 处理扩展信息 */
                    $content_info[$k]['extra'] = $v['extra'] ? json_decode($v['extra'], true) : array();
                    
                    /* 处理时间 */
                    $content_info[$k]['ctime'] = Util_Timer::formatMicrotime($v['ctime'], 'Y-m-d H:i:s');
                }
            }
            
            $data = array(
                    'folder_info' => $folder_info,
                    'content_info' => $content_info
            );
            $this->setData('data', $data);
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('查询失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
    
    /**
     * 获取指定行动及目录下的所有子级目录
     */
    public function getFoldersForSelect()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $card_id = $this->input->query('card_id');
        $folder_hid = $this->input->query('folder_hid');
        
        try {
            /* 检查请求方式 */
            if (! $this->input->isAJAX()) {
                throw new Zeed_Exception('请求方式错误');
            }
            
            /* 检查请求参数 */
            if (! $card_id || ! $folder_hid) {
                throw new Zeed_Exception('缺少参数，或参数错误');
            }
            
            /* 处理 folder_hid */
            $hids = explode(':', $folder_hid);
            $hid = $hids[0] . ':' . $hids[1];
            
            /* 执行查询 */
            $data['folders_forselect'] = Attachment_Model_Folder::instance()->getAllFoldersForSelect($hid, $card_id);
            
            $this->setData('data', $data);
        } catch (Zeed_Exception $e) {
            $this->setStatus($e->getCode() ? $e->getCode() : 1);
            $this->setError('查询失败 : ' . $e->getMessage());
        }
        
        return self::RS_SUCCESS;
    }
}

// End ^ LF ^ encoding
